package com.deyuanyun.pic.ctrl.service.impl;

import com.alibaba.fastjson.JSON;
import com.deyuanyun.pic.common.util.AopTargetUtils;
import com.deyuanyun.pic.ctrl.controller.dto.EqptCppowerDTO;
import com.deyuanyun.pic.ctrl.controller.vo.query.EqptQueryVO;
import com.deyuanyun.pic.ctrl.controller.vo.query.OperatorRecordsQueryVO;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.mapper.EqptCppowerrMapper;
import com.deyuanyun.pic.ctrl.mapper.OperationRecordsMapper;
import com.deyuanyun.pic.ctrl.mapper.RecodeCpPowerMapper;
import com.deyuanyun.pic.ctrl.service.OperationRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  阴保运行管理---运行记录service接口实现类
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-09-18
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class OperationRecordsServiceImpl implements OperationRecordsService {

    @Autowired EqptCppowerrMapper eqptCppowerrMapper; //阴保电源基础信息mapper

    @Autowired RecodeCpPowerMapper recodeCpPowerMapper; //阴保电源运行记录

    @Autowired OperationRecordsMapper operationRecordsMapper; //运行记录

    @Override
    public List queryCppowers(EqptQueryVO eqptQueryVO) {
        return operationRecordsMapper.queryEqptCppowers(eqptQueryVO);
    }

    private String[] methodStrs = {"queryRecodeCppowers", "queryRecodeReferences", "queryRecodeAnodesbeds", "queryRecodeInsedJoints",
            "queryRecodeAntiSurges", "queryRecodeCpjumpers", "queryRecodeCptestStations", "queryRecodeSaalAnodes", "queryRecodeDrainages"};

    @Override
    public List queryRecodes(Object obj, int methodIndex) {
        try {
            Object mapper = AopTargetUtils.getTarget(operationRecordsMapper);
            Method method = ReflectionUtils.findMethod(mapper.getClass(), methodStrs[methodIndex], OperatorRecordsQueryVO.class);
            return (List) method.invoke(mapper, obj);
        }  catch (Exception e) {
           throw new RuntimeException(e);
        }
    }

    @Override
    public Map<String, Object> queryEqptRecordCount(EqptTypeEnum eqptType, OperatorRecordsQueryVO recordsBaseVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(eqptType.equals(EqptTypeEnum.CPPOWER)){

            //统计阴保电源电压、电源、电流
            map.put("cppowerCount",operationRecordsMapper.queryCppowerCount(recordsBaseVO));
            //运行状态所占比
            List<Map<String,Object>> stateList = operationRecordsMapper.queryRunningStatedCount(recordsBaseVO);
            for(Map<String, Object> m:stateList){
                if(m.get("TYPE") == null)
                {
                    m.put("TYPE","正常运行");
                }
            }
            map.put("cpwStatedCount",stateList);
        }else if(eqptType.equals(EqptTypeEnum.CPTESTSTATION)){
            map.put("cptsPotentialCount",operationRecordsMapper.queryCptestStationPotentialCount(recordsBaseVO));
        }
        return map;
    }

}
